import {reqUserLogin} from '../api/index'
const state={
    uname:sessionStorage.getItem('uname'),
    token:sessionStorage.getItem('token'),
}
const mutations={
    LOGIN(state,result){
        state.token=result.token
        state.uname=result.uname
    },
    LOGOUT(state){
        state.token=''
        state.uname=''
    }
}
const actions={
    // 用户登录
    async login({commit},user){
        let result = await reqUserLogin(user)
        console.log(result);
        if(result.status==0){
            commit('LOGIN',result)
            // 持久化存储token
            sessionStorage.setItem('token',result.token)
            sessionStorage.setItem('uname',result.uname)

        }else{
            // return Promise.reject(new Error('faile'))
            alert('用户名或密码错误')
        }
    },
    // 用户退出
    logout({commit}){
        commit('LOGOUT')
    }
}
const getters={

}
export default{
    state,
    actions,
    mutations,
    getters
}